<%@ page language="java" pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
    request.setAttribute("basePath",basePath);
%>
<!DOCTYPE>
<html>
<head>
    <title>设备三码管理</title>
    <%@ include file="/resources/js/easyui/taglibs.jsp"%>
    <%@ include file="/resources/js/easyui/_easyui.jsp"%>
    <%@ include file="/resources/js/easyui/_message.jsp"%>
    <%@ include file="/resources/js/easyui/_viewJS.jsp"%>
</head>

<body>

<div id="tb" style="padding:5px;height:auto;">
    <table>
        <tr>
            <td><label style="margin-left:5px">设备简码:</label></td>
            <td>
                <input style="width:150px" id="shortCode" class="easyui-textbox" />
            </td>
            <td><label style="margin-left:5px">IMEI/MAC:</label></td>
            <td>
                <input style="width:150px" id="imei" class="easyui-textbox" />
            </td>
            <td>
                <a href="javascript:;" style="margin-left: 10px;vertical-align:middle" class="easyui-linkbutton" iconCls="icon-search" id="queryDataBtn">查询</a>
            </td>
        </tr>
    </table>
</div>

<table id="dg"
       title="设备三码管理"
       fitColumns="true"
       singleSelect="true"
       data-options="
       pageSize:10,
       toolbar:'#tb',
       pagination:true,
       collapsible:true,
       url:'${basePath}web/device/deviceList.json',
       method:'GET'">
    <thead>

    <tr>
        <th data-options="field:'deviceId',hidden:'true',width:10" ></th>
        <th data-options="field:'deviceName' ,width:100" align="center">设备名称</th>
        <th data-options="field:'shortCode' ,width:100" align="center" >设备简码</th>
        <th data-options="field:'imme' ,width:100" align="center" >IMEI/MAC</th>
        <th data-options="field:'userName' ,width:100" align="center" >用户名</th>
        <th data-options="field:'phone' ,width:100" align="center" >手机</th>
        <th data-options="field:'downCode' ,width:100,formatter:downCodeFormatter" align="center" >操作</th>
    </tr>

    </thead>
</table>

</body>

<script type="text/javascript">
    function View(){
        var that = this;

        that.events = [
            { sel : '#queryDataBtn', act : 'click', func : 'queryData' }
        ];

        that.$dataGrid = $('#dg');

        that.dataGridOption = {
            queryParams : {
                deviceshortcode : function(){return $('#shortCode').val();},
                imme : function(){return $('#imei').val();}
            }
        };

        $.extend(that.dataGridOption, ViewHelper.template.gridOption);

        that.$dataGrid.datagrid(that.dataGridOption);

        that.queryData = function () {
            that.$dataGrid.datagrid(that.dataGridOption);
        };

        that.downCodeZip = function(){
            that.$dataGrid.datagrid({
                onClickRow:function(index,data){
                    var rowData = that.$dataGrid.datagrid('getSelected');
                    console.dir(rowData);
                    $.ajax({
                        url:'${basePath}web/device/downCodeZip',
                        type : 'POST',
                        data : rowData,
                        async : false,
                        traditional:true,
                        error: function (response) {
                            ViewHelper.func.ajaxErrorMessage(response);
                        },
                        success: function (ret) {
                            if(ret.result) {
                                window.location.href = '${basePath}web/device/download';
                            }else{
                                $.messager.alert('提示','下载失败!','error');
                            }
                        }
                    });
                }
            })
        };

        EventHandler.call(that, that.events);

        return that;
    }

    var view;

    $(function(){
        inherit(View, EventHandler);
        view = new View();
    });

    function downCodeFormatter(value, rowData, rowIndex) {
        return '<a href="#" class="easyui-linkbutton" onclick="view.downCodeZip()">下载</a>';
    }
</script>

</html>